"use strict";

exports.__esModule = true;
exports.requestAuth = requestAuth;
exports.checkHasAuth = checkHasAuth;
exports.authLogin = authLogin;
var app = getApp();

/**
 * 请求用户权限
 *
 * @param key
 * @param notice
 */
function requestAuth(key, notice) {
    return new Promise(function (resolve, reject) {
        if (checkHasAuth()) {
            app.globalData.globalUserInfo = wx.getStorageSync("globalUserInfo");
            app.globalUserInfo = app.globalData.globalUserInfo;
            resolve();
        }
        wx.getSetting({
            success: function success(res) {
                if (!res.authSetting[key]) {
                    wx.authorize({
                        scope: key,
                        success: function success() {
                            resolve();
                        },
                        fail: function fail(e) {
                            // console.log("需要给与权限");
                            wx.showModal({
                                title: notice,
                                showCancel: false,
                                success: function success() {
                                    wx.openSetting({
                                        success: function success(res) {
                                            if (!res.authSetting[key]) {
                                                reject();
                                            } else {
                                                resolve();
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    });
                } else {
                    resolve();
                }
            },
            fail: function fail(e) {
                console.log(e);
                reject();
            }
        });
    });
}

function checkHasAuth() {
    return !!wx.getStorageSync("globalUserInfo");
}

function authLogin() {
    return new Promise(function (resolve, reject) {
        wx.login({
            success: function success(res) {
                if (res.code) {
                    resolve(res.code);
                }
            },
            fail: function fail() {
                reject();
            }
        });
    });
}